<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('参股基金')" prop="dealId">
            <el-select v-model="form.dealId" :disabled="form.id!==undefined" filterable clearable :placeholder="$t('请选择')">
              <el-option
                v-for="item in dealNameOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('估值日期')" prop="valuationDate">
            <el-date-picker
              v-model="form.valuationDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('估值日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司整体估值') + $t('金额单位')" prop="companyWholeVal">
            <Money v-model="form.companyWholeVal" unit="w" :placeholder="$t('公司整体估值')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目估值') + $t('金额单位')" prop="valuationResult">
            <Money v-model="form.valuationResult" unit="w" :placeholder="$t('项目估值)')" @change="changeVR(); changeVT()" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('估值方法')" prop="equityValMode">
            <el-select v-model="form.equityValMode" value="" :placeholder="$t('估值方法')">
              <el-option
                v-for="dict in equityValModeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('应对方案')" prop="responsePlan">
            <el-input v-model="form.responsePlan" type="textarea" :placeholder="$t('应对方案')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="company_projectVal" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="company:projectVal" />
    </div>
  </div>
</template>

<script>
    import { saveCompanyProjectValuation, getCompanyProjectValuation } from '@/api/company/projectVal/companyProjectValuation'
    import { lisDealName } from '@/api/bf/investGroup/bfInvestGroup'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  fileList: []
                },
                dealNameOptions: [],
                // 遮罩层
                loading: false,
                equityValModeOptions: [],
                // 表单校验
                rules: {
                  dealId: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  valuationDate: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  valuationResult: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  // equityValMode: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  companyWholeVal: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
              // 查询公司指标参数
              queryParams: {
                 indirectDealId: this.companyId || this.$route.query.companyId,
                 source: 'BF_PORTFOLIO'
              }
            }
        },
        created() {
          this.getDicts('valuation_method').then(response => {
            this.equityValModeOptions = response.data
          })
          // 获取所有关联项目
          lisDealName(this.queryParams).then(response => {
            this.dealNameOptions = response.rows
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    this.loading = true
                    getCompanyProjectValuation(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    companyId: this.$route.query.companyId,
                    dealId: this.$route.query.dealId,
                    latest: undefined,
                    valuationDate: undefined,
                    valuationResult: undefined,
                    companyWholeVal: undefined,
                    equityValMode: undefined,
                    responsePlan: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    fileList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveCompanyProjectValuation(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
